//
//  ZBStatusCellViewModel.swift
//  ZBWb
//
//  Created by 天隐翎 on 16/4/16.
//  Copyright © 2016年 csdn. All rights reserved.
//

import UIKit

class ZBStatusCellViewModel: NSObject {
    //在初始化方法 或者didSet内部给 status赋值 不会调用didSet
    var status: ZBStatus?
    //模型中的 mbrank不能够直接用于视图显示 需要进行额外的处理
    var mbImage: UIImage?
    //认证类型的图片
    var verifiedImage: UIImage?
    
    //转发的文字
    var repostTitle: String?
    //评论的文字
    var commentTitle: String?
    //赞的文字
    var ohYeahTitle: String?
    
    var headImageURL: NSURL?
//        {
//        return NSURL(string: status?.user?.profile_image_url ?? "")
//    }
    
    init(Status:ZBStatus) {
        super.init()
        self.status = Status
//        给headImageURL赋值
        headImageURL=NSURL(string: status?.user?.profile_image_url ?? "")
        
        //用户等级
        dealmbRank()
        //用户的认证类型
        dealVerifiedType()
        
        repostTitle = dealToolBarText(Status.reposts_count ?? 0, defaultTitle: "转发")
        commentTitle = dealToolBarText(Status.comments_count ?? 0, defaultTitle: "评论")
        ohYeahTitle = dealToolBarText(Status.attitudes_count ?? 0, defaultTitle: "赞")
    }
}
//处理微博数据
extension ZBStatusCellViewModel {
    
    //根据数量计算文字 如果大于0 就显示数量 如果 <= 0 就显示原有的文字
    private func dealToolBarText(count: Int, defaultTitle: String) -> String {
        //测试数据
        //        let count = 121000
        if count <= 0 {
            return defaultTitle
        }
        if count > 10000 {
            return "\(Double(count / 1000) / 10)万"
        }
        return "\(count)"
    }
    
    private func dealmbRank() {
        let mbType = status?.user?.mbrank ?? 0
        if mbType > 0 && mbType < 7 {
            mbImage = UIImage(named: "common_icon_membership_level\(mbType)")
//            print("common_icon_membership_level\(mbType)")
        }
    }
    
    private func dealVerifiedType() {
        //用户认证类型
        let verifiedType = status?.user?.verified ?? -1
        switch verifiedType {
        case 1:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = UIImage(named: "avatar_vip")
        }        
    }
}




